{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "metadata": false
    }
   },
   "source": [
    "# FL Server Joining FL experiment\n",
    "\n",
    "The purpose of this notebook is to show how to start a server to participate in an FL experiment."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "metadata": false
    }
   },
   "source": [
    "## Prerequisites\n",
    "- The [Startup notebook](1-Startup.ipynb) has been run successfully."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "metadata": false
    }
   },
   "source": [
    "### Start Server Docker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from IPython.display import HTML\n",
    "from multiprocessing import Process"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "running cmd demo_workspace/server/startup/docker.sh\n",
      "Starting docker with monai_nvflare:latest\n",
      "\n",
      "=============\n",
      "== PyTorch ==\n",
      "=============\n",
      "\n",
      "NVIDIA Release 21.08 (build 26011915)\n",
      "PyTorch Version 1.10.0a0+3fd9dcf\n",
      "\n",
      "Container image Copyright (c) 2021, NVIDIA CORPORATION.  All rights reserved.\n",
      "\n",
      "Copyright (c) 2014-2021 Facebook Inc.\n",
      "Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert)\n",
      "Copyright (c) 2012-2014 Deepmind Technologies    (Koray Kavukcuoglu)\n",
      "Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu)\n",
      "Copyright (c) 2011-2013 NYU                      (Clement Farabet)\n",
      "Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston)\n",
      "Copyright (c) 2006      Idiap Research Institute (Samy Bengio)\n",
      "Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz)\n",
      "Copyright (c) 2015      Google Inc.\n",
      "Copyright (c) 2015      Yangqing Jia\n",
      "Copyright (c) 2013-2016 The Caffe contributors\n",
      "All rights reserved.\n",
      "\n",
      "NVIDIA Deep Learning Profiler (dlprof) Copyright (c) 2021, NVIDIA CORPORATION.  All rights reserved.\n",
      "\n",
      "Various files include modifications (c) NVIDIA CORPORATION.  All rights reserved.\n",
      "\n",
      "This container image and its contents are governed by the NVIDIA Deep Learning Container License.\n",
      "By pulling and using the container, you accept the terms and conditions of this license:\n",
      "https://developer.nvidia.com/ngc/nvidia-deep-learning-container-license\n",
      "\n",
      "NOTE: MOFED driver for multi-node communication was not detected.\n",
      "      Multi-node communication performance may be reduced.\n",
      "\n",
      "\u001b]0;root@sys: /workspace\u0007root@sys:/workspace# "
     ]
    }
   ],
   "source": [
    "server_name = \"server\"\n",
    "workspace = \"demo_workspace\"\n",
    "\n",
    "server_startup_path = os.path.join(workspace, server_name, \"startup\")\n",
    "cmd = server_startup_path + \"/docker.sh\"\n",
    "\n",
    "\n",
    "def run_server():\n",
    "    cmd = server_startup_path + \"/docker.sh\"\n",
    "    print(\"running cmd \" + cmd)\n",
    "    !$cmd\n",
    "\n",
    "\n",
    "p1 = Process(target=run_server)\n",
    "\n",
    "p1.start()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Check Started Containers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS        PORTS     NAMES\n",
      "08492335d003   monai_nvflare:latest   \"/usr/local/bin/nvid…\"   2 seconds ago   Up 1 second             flserver\n"
     ]
    }
   ],
   "source": [
    "!docker ps -a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Start Server"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To start a server, you should:\n",
    "\n",
    "- open a terminal and enter the container named `flserver`.\n",
    "- run `start.sh` under `startup/`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<a href=\"\", data-commandlinker-command=\"terminal:create-new\"> Open a new terminal</a>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# You can click the following link, or manually open a new terminal.\n",
    "HTML('<a href=\"\", data-commandlinker-command=\"terminal:create-new\"> Open a new terminal</a>')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The commands can be:\n",
    "\n",
    "```\n",
    "docker exec -it flserver bash\n",
    "cd startup/\n",
    "sh start.sh\n",
    "```\n",
    "\n",
    "A successfully started server will print logs as follow:\n",
    "<br>![fl](demo_figs/enter_server_success.png)<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Next Steps\n",
    "\n",
    "You have now started the server container.\n",
    "In the next notebook, [Client Startup Notebook](3-Client.ipynb), you'll start two clients participating in the FL experiment."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "stem_cell": {
   "cell_type": "raw",
   "metadata": {
    "pycharm": {
     "metadata": false
    }
   },
   "source": ""
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
